package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.LikesBehaviorDto;
import com.heima.model.behavior.ReadBehaviorDto;
import com.heima.model.behavior.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author lzq
 * @data 2023/5/10 14:17
 */
@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {
    //点赞
   public static final String LIKES_BEHAVIOR="likes.Behavior";
   //阅读
   public static final String READ_BEHAVIOR="read.behavior";
   //喜欢不喜欢
   public static final String UN_LIKE_BEHAVIOR="un.likes.behavior";
    @Autowired
    private CacheService cacheService;
    @Autowired
    private KafkaTemplate kafkaTemplate;
    /**
     * 文章点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        //参数校验
        if (dto.getArticleId()==null||dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前用户
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //创建mess对象进行流数据聚合处理
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());
        if (dto.getOperation()==0){
            //点赞操作
            //查询是否已经点赞
            Object o = cacheService.hGet(LIKES_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
            if (o!=null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"您已点赞");
            }
            //保存到redis
            cacheService.hPut(LIKES_BEHAVIOR + dto.getArticleId().toString(),user.getId().toString(), JSON.toJSONString(dto));
            mess.setAdd(1);
        }else {
            //取消点赞,删除操作
            mess.setAdd(-1);
            cacheService.hDelete(LIKES_BEHAVIOR + dto.getArticleId().toString(),user.getId().toString());
        }
        //发送到kafka,进行流数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读量
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //无效参数
        if (dto==null||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        //是否登录判断
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取原先阅读次数
        String readJson = (String) cacheService.hGet(READ_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
        if (StringUtils.isNotBlank(readJson)){
            ReadBehaviorDto rdto = JSON.parseObject(readJson, ReadBehaviorDto.class);
            int count = rdto.getCount() + dto.getCount();
            dto.setCount(count);
        }
        //保存到redis
        cacheService.hPut(READ_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(),JSON.toJSONString(dto));
        //发送到kafka,进行流数据聚合
        //创建mess对象进行流数据聚合处理
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 喜欢,不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        //参数校验
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (dto.getType() == 0) {
            //喜欢
            cacheService.hPut(UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
        } else {
           //不喜欢
            cacheService.hDelete(UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
